package com.ruoyi.web.controller.test;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ContractPayment;
import com.ruoyi.system.service.IContractPaymentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 合同收款-CRUDController
 *
 * @author ruoyi
 * @date 2022-08-02
 */
@RestController
@RequestMapping("/system/payment")
public class ContractPaymentController extends BaseController
{
    @Autowired
    private IContractPaymentService contractPaymentService;

    /**
     * 查询合同收款-CRUD列表
     */
    @PreAuthorize("@ss.hasPermi('system:payment:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContractPayment contractPayment)
    {
        startPage();
        List<ContractPayment> list = contractPaymentService.selectContractPaymentList(contractPayment);
        return getDataTable(list);
    }

    /**
     * 导出合同收款-CRUD列表
     */
    @PreAuthorize("@ss.hasPermi('system:payment:export')")
    @Log(title = "合同收款-CRUD", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContractPayment contractPayment)
    {
        List<ContractPayment> list = contractPaymentService.selectContractPaymentList(contractPayment);
        ExcelUtil<ContractPayment> util = new ExcelUtil<ContractPayment>(ContractPayment.class);
        util.exportExcel(response, list, "合同收款-CRUD数据");
    }

    /**
     * 获取合同收款-CRUD详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:payment:query')")
    @GetMapping(value = "/{paymentId}")
    public AjaxResult getInfo(@PathVariable("paymentId") Long paymentId)
    {
        return AjaxResult.success(contractPaymentService.selectContractPaymentByPaymentId(paymentId));
    }

    /**
     * 新增合同收款-CRUD
     */
    @PreAuthorize("@ss.hasPermi('system:payment:add')")
    @Log(title = "合同收款-CRUD", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContractPayment contractPayment)
    {
        contractPayment.setUserId(getUserId());
        contractPayment.setDeptId(getDeptId());
        return toAjax(contractPaymentService.insertContractPayment(contractPayment));
    }

    /**
     * 修改合同收款-CRUD
     */
    @PreAuthorize("@ss.hasPermi('system:payment:edit')")
    @Log(title = "合同收款-CRUD", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContractPayment contractPayment)
    {
        return toAjax(contractPaymentService.updateContractPayment(contractPayment));
    }

    /**
     * 删除合同收款-CRUD
     */
    @PreAuthorize("@ss.hasPermi('system:payment:remove')")
    @Log(title = "合同收款-CRUD", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paymentIds}")
    public AjaxResult remove(@PathVariable Long[] paymentIds)
    {
        return toAjax(contractPaymentService.deleteContractPaymentByPaymentIds(paymentIds));
    }
    /**
     * 导入数据管理列表
     */
    @Log(title = "数据管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ContractPayment> util = new ExcelUtil<ContractPayment>(ContractPayment.class);
        List<ContractPayment> contractPaymentList = util.importExcel(file.getInputStream());
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = getUsername();
        String message = contractPaymentService.importPayment(contractPaymentList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ContractPayment> util = new ExcelUtil<ContractPayment>(ContractPayment.class);
        util.importTemplateExcel(response,"案卷数据");
    }
}
